#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_ttf.h>

void affImage(int x, int y, SDL_Surface* src, SDL_Surface* dst){

SDL_Rect rect;

rect.x = x;
rect.y = y;


SDL_BlitSurface(src, NULL, dst, &rect);

}


int main( int argc, char *argv[] )
{


SDL_Surface *ecran, *bg,*dot;
int gameStarted = 1;
int dotx=0, doty=560, bgx=0, bgy=0;
SDL_Event event;

if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
    {
        printf( "Can't init SDL:  %s\n", SDL_GetError( ) );
        return EXIT_FAILURE;
    }

    atexit( SDL_Quit );
    ecran = SDL_SetVideoMode( 800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    
    if( ecran == NULL )
    {
        printf( "Can't set video mode: %s\n", SDL_GetError( ) );
        return EXIT_FAILURE;
    }

bg = SDL_LoadBMP("bg.bmp");
dot = SDL_LoadBMP("dot.bmp");

SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

while (gameStarted)
    {
/* Remplissage de la couleur du Background */
SDL_FillRect(ecran, 0, SDL_MapRGB(ecran->format, 255,255, 255));

/* ********* BEGIN récupération des événements ************ */
       
      while(SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                gameStarted = 0;
                break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_RIGHT:
if(dotx+dot->w<800){
dotx+=1;
}
break;

case SDLK_DOWN:
if(doty+dot->h<600){
doty+=10;
}
break;
case SDLK_UP:
doty-=4;
break;
case SDLK_LEFT:
dotx-=4;
break;

case SDLK_SPACE:
doty-=150;
break;
}

		}
	}

bgx-=5;
if(bgx <= -800)
	bgx=0;

if(doty+dot->h<600){
doty+=2;
}
affImage(bgx,bgy,bg,ecran);
affImage(bgx+bg->w,bgy,bg,ecran);
affImage(dotx,doty,dot,ecran);

SDL_Flip(ecran);
}
return 0;
}


